<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>09-阴影</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    html,
    body {
      width: 100%;
      height: 100%;
    }
  </style>
</head>

<body>
  <script type="module">
    import * as THREE from "three";
    import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
    // 创建场景
    const scene = new THREE.Scene();
    // 创建相机
    const camera = new THREE.PerspectiveCamera(
      45,
      window.innerWidth / window.innerHeight,
      1,
      1000
    );
    camera.position.set(0, 10, 50);
    camera.lookAt(0, 0, 0);
    // 创建渲染器 
    const renderer = new THREE.WebGLRenderer({
      antialias: true
    });
    // 设置渲染器大小
    renderer.setSize(window.innerWidth, window.innerHeight);
    // 渲染器开启阴影支持
    renderer.shadowMap.enabled = true;
    // 把渲染的canvas内容添加到body
    document.body.appendChild(renderer.domElement);

    // 环境光
    const ambientLight = new THREE.AmbientLight(0x404040); // 柔和的白光
    scene.add(ambientLight);

    // 平行光(有投影)
    const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
    directionalLight.castShadow = true;
    directionalLight.shadow.mapSize.width = 2048;
    directionalLight.shadow.mapSize.height = 2048;
    directionalLight.position.set(150, 20, 0);

    const directionalLightHelper = new THREE.DirectionalLightHelper(
      directionalLight,
      5
    );
    // scene.add(directionalLight, directionalLightHelper);
    
    // 点光源(有投影)
    const pointLight = new THREE.PointLight(0xffffff, 200, 100);
    pointLight.castShadow = true;
    pointLight.position.set(10, 10, 0);

    const ponitLightHelper = new THREE.PointLightHelper(pointLight, 1);
    scene.add(pointLight, ponitLightHelper);
    

    /*
      阴影投射: 光源 物体 渲染器
    */
    // 地面
    const plane = new THREE.Mesh(
      new THREE.PlaneGeometry(100, 100),
      new THREE.MeshStandardMaterial({
        color: 0xffffff
      })
    );
    plane.rotation.x = -Math.PI / 2;
    plane.receiveShadow = true;
    scene.add(plane);

    // 立方体
    const cube = new THREE.Mesh(
      new THREE.BoxGeometry(10, 10, 10),
      new THREE.MeshStandardMaterial({
        color: 0xff0000
      })
    );
    cube.position.set(0, 5, 0);
    cube.castShadow = true;
    scene.add(cube);

    // 创建轨道控制器
    const controls = new OrbitControls(camera, renderer.domElement);

    // 开启物体的阻尼动画效果 
    controls.enableDamping = true;

    // 监听controls的change事件
    controls.addEventListener("change", () => {
      // 交互的鼠标每次发生变化，都需要渲染一次(如果用了animate函数就不需要).
      renderer.render(scene, camera);
    });
    renderer.render(scene, camera);

    let angle = 0;
    function animate() {
      angle += 0.01;
      pointLight.position.x = 10 * Math.cos(angle);
      pointLight.position.z = 10 * Math.sin(angle);

      requestAnimationFrame(animate);
      controls.update();
      renderer.render(scene, camera);
    } 

    // 添加动画并渲染
    animate();
  </script>
</body>

</html>